package com.cheney.demo.data;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.*;

/**
 * @author si.chen
 * @date 2024/12/22 19:25
 */
public class KKK差异_05 {
    public static void main(String[] args) throws IOException {
        // 读取输入Excel文件
        List<String[]> inputData = readExcelFile("D:/tmp/import5.xlsx");
        
        // 使用Map存储每列元素及其出现次数
        Map<String, Integer> aMap = new HashMap<>();
        Map<String, Integer> bMap = new HashMap<>();
        Map<String, Integer> cMap = new HashMap<>();
        
        // 跳过标题行，统计每列元素出现次数
        for (int i = 1; i < inputData.size(); i++) {
            String[] row = inputData.get(i);
            if (!row[0].trim().isEmpty()) {
                aMap.merge(row[0].trim(), 1, Integer::sum);
            }
            if (!row[1].trim().isEmpty()) {
                bMap.merge(row[1].trim(), 1, Integer::sum);
            }
            if (!row[2].trim().isEmpty()) {
                cMap.merge(row[2].trim(), 1, Integer::sum);
            }
        }
        
        // 计算各种组合的交集
        Set<String> abcIntersection = new HashSet<>(aMap.keySet());
        abcIntersection.retainAll(bMap.keySet());
        abcIntersection.retainAll(cMap.keySet());
        
        Set<String> abIntersection = new HashSet<>(aMap.keySet());
        abIntersection.retainAll(bMap.keySet());
        abIntersection.removeAll(abcIntersection);
        
        Set<String> acIntersection = new HashSet<>(aMap.keySet());
        acIntersection.retainAll(cMap.keySet());
        acIntersection.removeAll(abcIntersection);
        
        Set<String> bcIntersection = new HashSet<>(bMap.keySet());
        bcIntersection.retainAll(cMap.keySet());
        bcIntersection.removeAll(abcIntersection);
        
        // 创建输出工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Result");
        
        // 写入标题行
        Row headerRow = sheet.createRow(0);
        String[] headers = {"a", "a重复数", "b", "b重复数", "c", "c重复数", 
                          "abc重合", "ab重合", "ac重合", "bc重合"};
        for (int i = 0; i < headers.length; i++) {
            headerRow.createCell(i).setCellValue(headers[i]);
        }
        
        // 准备所有数据列表
        List<Map.Entry<String, Integer>> aEntries = new ArrayList<>(aMap.entrySet());
        List<Map.Entry<String, Integer>> bEntries = new ArrayList<>(bMap.entrySet());
        List<Map.Entry<String, Integer>> cEntries = new ArrayList<>(cMap.entrySet());
        List<String> abcList = new ArrayList<>(abcIntersection);
        List<String> abList = new ArrayList<>(abIntersection);
        List<String> acList = new ArrayList<>(acIntersection);
        List<String> bcList = new ArrayList<>(bcIntersection);
        
        // 获取最大行数
        int maxRows = Math.max(Math.max(Math.max(aMap.size(), bMap.size()),
                Math.max(cMap.size(), abcIntersection.size())),
                Math.max(Math.max(abIntersection.size(), acIntersection.size()),
                        bcIntersection.size()));
        
        // 写入数据
        for (int i = 0; i < maxRows; i++) {
            Row row = sheet.createRow(i + 1);
            
            // 写入a列数据
            if (i < aEntries.size()) {
                row.createCell(0).setCellValue(aEntries.get(i).getKey());
                row.createCell(1).setCellValue(aEntries.get(i).getValue());
            }
            
            // 写入b列数据
            if (i < bEntries.size()) {
                row.createCell(2).setCellValue(bEntries.get(i).getKey());
                row.createCell(3).setCellValue(bEntries.get(i).getValue());
            }
            
            // 写入c列数据
            if (i < cEntries.size()) {
                row.createCell(4).setCellValue(cEntries.get(i).getKey());
                row.createCell(5).setCellValue(cEntries.get(i).getValue());
            }
            
            // 写入交集数据
            if (i < abcList.size()) {
                row.createCell(6).setCellValue(abcList.get(i));
            }
            if (i < abList.size()) {
                row.createCell(7).setCellValue(abList.get(i));
            }
            if (i < acList.size()) {
                row.createCell(8).setCellValue(acList.get(i));
            }
            if (i < bcList.size()) {
                row.createCell(9).setCellValue(bcList.get(i));
            }
        }
        
        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }
        
        // 保存文件
        try (FileOutputStream fileOut = new FileOutputStream("D:/tmp/export5.xlsx")) {
            workbook.write(fileOut);
        }
        workbook.close();
    }
    
    private static List<String[]> readExcelFile(String filePath) throws IOException {
        List<String[]> data = new ArrayList<>();
        try (FileInputStream inputStream = new FileInputStream(new File(filePath));
             Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                String[] rowData = new String[3];
                for (int i = 0; i < 3; i++) {
                    Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    if (cell.getCellType() == CellType.BLANK) {
                        rowData[i] = "";
                    } else if (cell.getCellType() == CellType.NUMERIC) {
                        rowData[i] = String.format("%.0f", cell.getNumericCellValue());
                    } else {
                        rowData[i] = cell.toString().trim();
                    }
                }
                data.add(rowData);
            }
        }
        return data;
    }
}
